package com.example.auth.service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * sms service
 *
 * @author cui haida
 * @version JDK 8
 * 2025/8/5
 */
@Slf4j
@Service
public class SmsService {
    private final StringRedisTemplate redisTemplate;

    public SmsService(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * send sms
     */
    public void sendSmsCode(String phone) {
        String code = generateRandomCode(6);
        // cache the code
        redisTemplate.opsForValue().set("SMS_CODE:" + phone, code, 5, TimeUnit.MINUTES);
        // send the sms
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);
        String signName = "sms test";
        String templateCode = "SMS_174989915";
        String templateParam = "{\"code\":\"" + code + "\"}";
        // create and send
        CommonRequest request = getCommonRequest(phone, signName, templateCode, templateParam);
        try {
            // get response
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            log.error("send sms error, {}", e.getMessage());
        }
    }

    /**
     * get aliyun sms request params
     * @param phoneNumbers phone numbers
     * @param signName sign name
     * @param templateCode template code
     * @param templateParam template param
     * @return CommonRequest
     */
    private static CommonRequest getCommonRequest(String phoneNumbers, String signName, String templateCode, String templateParam) {
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", templateParam);
        return request;
    }

    /**
     * generate random code as phone code
     */
    private String generateRandomCode(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }
}